#!/opt/perl/bin/perl -w
#########################################################################
#                                                                       #
# Name:         check_env_v_prod.pl (Perl script)                       #
#                                                                       #
# Completed:    30th May, 2006.                                         #
#                                                                       #
# Updated:                                                              #
#                                                                       #
# Author:       Nev Lawrence, Proprius Consulting Ltd.                  #
#                                                                       #
# Arguments:    schedule_name [-v] -[d]                                 #
#		-v 	Verbose. List all jobs and commands		#
#		-d 	Debug. List the actual strings used for compare #
#			when a comparison finds a problem		#
#                                                                       #
# Purpose:      A script to compare the jobs in a given environment's 	#
#		Maestro schedule against the current production set of 	#
#		jobs. Any missings jobs and any differences in the 	#
#		command string are listed.				#
#		If the -v switch is set tehn all jobs and command	#
#		strings are listed.					#
#		When comapring command strings, strings such as PROD &	#
#		TEST are changed to hashes to allow valid comparisons	#
#		etc.							#
#                                                                       #
#########################################################################

use warnings;
use strict;
use Data::Dumper;
use File::Basename;
use Getopt::Std;

# Set constants
my $prg=basename($0);
my %opts;
my $testenv=undef;
my $mailid='neville.lawrence@bankofamerica.com';
my $composer_path="/appl/maestro/bin/composer";
my $prod_mst_host="spbe895a";
my $prod_mst_sched="spbe895a";
my $mst_jobs_file="/tmp/maestro.jobs.";
my $mst_parms_file="/tmp/maestro.jobs.";
my $line=undef;
my $prod_sched_job_type=undef;
my $prod_sched_job=undef;
my $prod_job=undef;
my $prod_job2=undef;
my $prod_type=undef;
my $prod_cmd=undef;
my $test_sched_job_type=undef;
my $test_sched_job=undef;
my $test_job=undef;
my $test_type=undef;
my $test_cmd=undef;
my $prod_cmd_hashed=undef;
my $test_cmd_hashed=undef;
my $fnd=undef;
my $opt_v=undef;
my $opt_d=undef;
my $verbose=undef;
my $debug=undef;

sub usage {
        print "\nUsage: $prg [-v] [-d] schedule_name

$prg compares the jobs in the given test Maestro 
schedule schedule_name against the current production schedule 
($prod_mst_sched) set of jobs. 

 -v	Verbose. List all jobs and commands.
 -d	Debug. List the actual strings used for compare when a 
	comparison finds a problem.

Written by Nev Lawrence 2006, proprius.co.uk
";
        exit 1;
}

# Parse command line args  
getopt('', \%opts);
if (defined $opts{"v"} == "1") { 
	$verbose=$opts{"v"};
}

if (defined $opts{"d"} == "1") { 
	$debug=$opts{"d"};
}

# Have we been passed a test schedule name?
if (! defined($ARGV[0])) {
        print "Schedule Name not specified\n";
        usage;
}
$testenv = $ARGV[0];

#
# Compare Tivoli schedule against Prod
#
# Gen a list of jobs in the given test env 
print "Generating list of jobs in test schedule $testenv...\n";
my $mst_jobs_file_test=$mst_jobs_file.$testenv;
system("echo \"create $mst_jobs_file_test from j=$testenv#@\" | $composer_path > /dev/null 2>&1"); 
my $rc=($?/256);;
if ($rc != 0) {
        print "Unable to create list of jobs for schedule $testenv\n";
        exit 2;
}

# Gen a list of jobs on production
print "\nGenerating list of jobs in production schedule $prod_mst_sched...\n";
my $mst_jobs_file_prod=$mst_jobs_file.$prod_mst_sched;
system("ssh $prod_mst_host echo \"create $mst_jobs_file_prod from j=$prod_mst_sched#@ | $composer_path\" > /dev/null 2>&1");

# Copy the list of prod jobs over to this machine
system("scp $prod_mst_host:$mst_jobs_file_prod /tmp/.") && die "Unable to invoke shell to run scp command!!\n";

# Read in both lists of jobs 1st
open (PROD_JOBS,"<$mst_jobs_file_prod");
my @prod_jobs=<PROD_JOBS>;

open (TEST_JOBS,"<$mst_jobs_file_test");
my @test_jobs=<TEST_JOBS>;

# Get the SCRIPTNAME and the DOCOMMAND lines only
my @prod_jobs2=grep(/SCRIPTNAME/ || /DOCOMMAND/,@prod_jobs);
my @test_jobs2=grep(/SCRIPTNAME/ || /DOCOMMAND/,@test_jobs);

# lets get on and comapre the jobs and command strings
(defined $verbose) && print "\nVerbose mode - listing all jobs and commands\n";
print "\nComparing production jobs against jobs in test schedule $testenv:\n";

# Break each prod line into the job name, type (SCRIPTNAME or DOCOMMAND)
#  and command string
foreach $line (@prod_jobs2) {
	chomp $line;
	($prod_sched_job_type, $prod_cmd)=split(/"/,$line);
	($prod_job, $prod_type)=split(/ /,$prod_sched_job_type);

	# strip off the schedule name
	my $l=length($prod_mst_host);
	substr($prod_job, 0, $l+1)="";

	(defined $verbose) && print "\nChecking production job $prod_job:\n"; 
	
	# Does the prod job exist in test?
	$fnd = grep(/$prod_job/, @test_jobs2);

	# If so does the command string match the test one?
	if ($fnd > 0 ) {
		my $i=0;
		my $fnd_indx=undef;
		for ($i=0; $i < @test_jobs2; $i++) {
			if (grep(/$prod_job/, $test_jobs2[$i])) {
				$fnd_indx = $i;
				last;
			}
		}

		# Break up the test line into the job name, type (SCRIPTNAME or DOCOMMAND)
		($test_sched_job_type, $test_cmd)=split(/"/,$test_jobs2[$fnd_indx]);
		($test_job, $test_type)=split(/ /,$test_sched_job_type);

		(defined $verbose) && print "Prod cmd: $prod_cmd\n";
		(defined $verbose) && print "Test cmd: $test_cmd\n";

		# Hash out the PROD and ENV_nn strings etc to ease comparison
		$prod_cmd_hashed=$prod_cmd;
		$prod_cmd_hashed=~ s/\^PROD\^/\^####\^/g;
       		$prod_cmd_hashed=~ s/\^ENV_01\^/\^ENV_NN\^/g;
       		$prod_cmd_hashed=~ s/\^DONE_PR\^/\^DONE_xx\^/g;
		$prod_cmd_hashed=~ s/Set_Env.prod-/Set_Env.####-/g;

		$test_cmd_hashed=$test_cmd;
		$test_cmd_hashed=~ s/\^TEST\^/\^####\^/g;
		$test_cmd_hashed=~ s/\^ENV_..\^/\^ENV_NN\^/g;
		$test_cmd_hashed=~ s/\^DONE_..\^/\^DONE_xx\^/g;
		$test_cmd_hashed=~ s/Set_Env.test-/Set_Env.####-/g;

		# Do the cmd strings match?
		if ("$test_cmd_hashed" ne "$prod_cmd_hashed") {
			(defined $verbose) || print "\nProduction job $prod_job - possible problem having compared the Command strings:\n"; 
			(defined $verbose) && print " - possible problem having compared the Command strings for job $prod_job\n";
			(defined $verbose) || print "Prod cmd: $prod_cmd\n";
			(defined $verbose) || print "Test cmd: $test_cmd\n";
			(defined $debug) && print "Actual strings compared:\n";
			(defined $debug) && print "Prod cmd: $prod_cmd_hashed\n";
			(defined $debug) && print "Test cmd: $test_cmd_hashed\n";
		} else {
			(defined $verbose) && print "- they match\n";
		}
	} else {
     		print "\nProduction job $prod_job - NOT found in $testenv\n";
		print "Prod cmd: $prod_cmd\n";
  	}
}

# Now get the PARMS in order to help make the report more readable
#print "\n\nMaestro parms:\n";
#system("echo \"display parms\" | $composer_path > $mst_parms_file 2>&1"); 
#system("echo \"display parms\" | $composer_path"); 
#$rc=($?/256);;
#if ($rc != 0) {
#        print "Unable to display Maestro parms\n";
#        exit 2;
#}

